package com.jinit.jia.boot;

import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * BeanDefinitionLoader
 *
 * @author JInit
 * @date 2021-08-06
 */
public class BeanDefinitionLoader {

    private final Object[] sources;

    private final AnnotatedBeanDefinitionReader annotatedReader;

    private final XmlBeanDefinitionReader xmlReader;

    private BeanDefinitionReader groovyReader;

    private final ClassPathBeanDefinitionScanner scanner;

    private ResourceLoader resourceLoader;

    public BeanDefinitionLoader(BeanDefinitionRegistry registry, Object... sources) {
        Assert.notNull(registry, "注册表不能为空");
        Assert.notEmpty(sources, "资源不能为空");
        this.sources = sources;
        // 注解形式的Bean定义读取器
        this.annotatedReader = new AnnotatedBeanDefinitionReader(registry);
        // XML形式的Bean定义读取器
        this.xmlReader = new XmlBeanDefinitionReader(registry);
        // 类路径扫描器
        this.scanner = new ClassPathBeanDefinitionScanner(registry);
        // 扫描器添加排除过滤器
        //this.scanner.addExcludeFilter(new ClassExcludeFilter(sources));
    }

    public int load() {
        int count = 0;
        for (Object source : this.sources) {
            count += load(source);
        }
        return count;
    }

    /**
     * 加载资源
     * 复用部分spring源码
     * @param source
     * @return
     */
    private int load(Object source) {
        Assert.notNull(source, "Source must not be null");
        // 从Class加载
        if (source instanceof Class<?>) {
            return load((Class<?>) source);
        }
        // 从Resource加载
        if (source instanceof Resource) {
            return load((Resource) source);
        }
        // 从Package加载
        if (source instanceof Package) {
            return load((Package) source);
        }
        // 从 CharSequence 加载
        if (source instanceof CharSequence) {
            return load((CharSequence) source);
        }
        throw new IllegalArgumentException("Invalid source type " + source.getClass());
    }

    private int load(Class<?> source) {
        if (isComponent(source)) {
            //将 启动类的 BeanDefinition注册进 beanDefinitionMap
            this.annotatedReader.register(source);
            return 1;
        }
        return 0;
    }

    private int load(Resource source) {
        if (source.getFilename().endsWith(".groovy")) {
            if (this.groovyReader == null) {
                throw new BeanDefinitionStoreException("Cannot load Groovy beans without Groovy on classpath");
            }
            return this.groovyReader.loadBeanDefinitions(source);
        }
        return this.xmlReader.loadBeanDefinitions(source);
    }

    private int load(Package source) {
        return this.scanner.scan(source.getName());
    }

    private int load(CharSequence source) {
        String resolvedSource = this.xmlReader.getEnvironment().resolvePlaceholders(source.toString());
        // Attempt as a Class
        try {
            return load(ClassUtils.forName(resolvedSource, null));
        }
        catch (IllegalArgumentException | ClassNotFoundException ex) {
            // swallow exception and continue
        }
        // Attempt as resources
        Resource[] resources = findResources(resolvedSource);
        int loadCount = 0;
        boolean atLeastOneResourceExists = false;
        for (Resource resource : resources) {
            if (isLoadCandidate(resource)) {
                atLeastOneResourceExists = true;
                loadCount += load(resource);
            }
        }
        if (atLeastOneResourceExists) {
            return loadCount;
        }
        // Attempt as package
        Package packageResource = findPackage(resolvedSource);
        if (packageResource != null) {
            return load(packageResource);
        }
        throw new IllegalArgumentException("Invalid source '" + resolvedSource + "'");
    }

    private Resource[] findResources(String source) {
        ResourceLoader loader = (this.resourceLoader != null) ? this.resourceLoader
                : new PathMatchingResourcePatternResolver();
        try {
            if (loader instanceof ResourcePatternResolver) {
                return ((ResourcePatternResolver) loader).getResources(source);
            }
            return new Resource[] { loader.getResource(source) };
        }
        catch (IOException ex) {
            throw new IllegalStateException("Error reading source '" + source + "'");
        }
    }

    private boolean isLoadCandidate(Resource resource) {
        if (resource == null || !resource.exists()) {
            return false;
        }
        if (resource instanceof ClassPathResource) {
            // A simple package without a '.' may accidentally get loaded as an XML
            // document if we're not careful. The result of getInputStream() will be
            // a file list of the package content. We double check here that it's not
            // actually a package.
            String path = ((ClassPathResource) resource).getPath();
            if (path.indexOf('.') == -1) {
                try {
                    return Package.getPackage(path) == null;
                }
                catch (Exception ex) {
                    // Ignore
                }
            }
        }
        return true;
    }

    private Package findPackage(CharSequence source) {
        Package pkg = Package.getPackage(source.toString());
        if (pkg != null) {
            return pkg;
        }
        try {
            // Attempt to find a class in this package
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(getClass().getClassLoader());
            Resource[] resources = resolver
                    .getResources(ClassUtils.convertClassNameToResourcePath(source.toString()) + "/*.class");
            for (Resource resource : resources) {
                String className = StringUtils.stripFilenameExtension(resource.getFilename());
                load(Class.forName(source.toString() + "." + className));
                break;
            }
        }
        catch (Exception ex) {
            // swallow exception and continue
        }
        return Package.getPackage(source.toString());
    }

    private boolean isComponent(Class<?> type) {
        // This has to be a bit of a guess. The only way to be sure that this type is
        // eligible is to make a bean definition out of it and try to instantiate it.
        if (MergedAnnotations.from(type, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY).isPresent(Component.class)) {
            return true;
        }
        // Nested anonymous classes are not eligible for registration, nor are groovy
        // closures
        return !type.getName().matches(".*\\$_.*closure.*") && !type.isAnonymousClass()
                && type.getConstructors() != null && type.getConstructors().length != 0;
    }
}
